﻿using System;
using System.ComponentModel;
using System.ComponentModel.Composition;

using SBPweb.PROvibe.Shared.Library.Components;
using SBPweb.PROvibe.Server.Library.Modularity;

using SBPweb.PROvibe.Server.Modules.ContentProvider.Contracts;

namespace SBPweb.PROvibe.Server.Modules.ContentProvider
{
    
    [Export(typeof(IModule))]
    public class ContentProviderModule : ServerModuleBase
    {

        #region Fields

        private static object lh = new object();
        private ContentProviderService service;

        #endregion

        #region Constructor logic

        public ContentProviderModule()
            : base()
        {
            lock (lh)
            {
                if (Instance == null)
                {
                    Instance = this;
                }
            }
            service = new ContentProviderService(this);
        }

        #endregion

        #region Properties

        internal static ContentProviderModule Instance
        {
            get;
            private set;
        }

        #endregion

        #region Methods

        protected override void OnServerContextAssigning()
        {
            base.OnServerContextAssigning();

            ServerContext.ServiceManager.RegisterService(service);
        }

        protected override void OnInitializing(CancelEventArgs e)
        {
            base.OnInitializing(e);

            ServiceProvider.Contracts.IServiceProviderService isps = PublicServices.FirstByType<ServiceProvider.Contracts.IServiceProviderService>();
            isps.CreateServiceHost<ContentProviderService, IContentProviderService>(this, "ContentProvider.svc");
        }

        protected override void OnTerminating(CancelEventArgs e)
        {
            base.OnTerminating(e);
        }

        #endregion
        
    }

}